package eightDataType.mStr;


import java.io.UnsupportedEncodingException;

/**
 * @author Ajie
 * @date 2019-08-22
 * @function 转16进制
 *
 *
 */
public class StringToHex16 {

//            int strToInt = Integer.parseInt(decStr_1);//太长了.比较短 可以
//            String s3 = Integer.toHexString(strToInt);


// 01 AB EC  卡号是一对一对。补0
// If odd length, then prepend a "0" in front
//                if (cardNo.length() % 2 == 1) {
//        cardNo = "0" + cardNo;
//        Log.i(TAG, "getType: add 0 in front");
//    }

//    static String printStr = "SIZE 54.0 mm, 85.6mm\n" +
    static String printStr = "SIZE 60.0 mm, 80 mm\n" +
            "GAP 2 mm, 0\n" +
            "SPEED 4.0\n" +
            "CLS\n" +
            "DIRECTION 0\n" +
            "REFERENCE 0,0\n" +
            "SHIFT 0\n" +
            "TEXT 360,200,\"TSS24.BF2\",90,1,1,\"黄鹤翔\"\n" +
            "TEXT 280,200,\"TSS24.BF2\",90,1,1,\"杭州蓝天科技有限公司公司\"\n" +
            "TEXT 200,200,\"TSS24.BF2\",90,1,1,\"2019-07-23 13:00\"\n" +
            "TEXT 120,200,\"TSS24.BF2\",90,1,1,\"李霞\"\n" +
            "PRINT 1,1\n" +
            "EOP\n";
//
//    static String printStr ="SIZE 40 mm, 30 mm\n" +
//            "GAP 2 mm, 0\n" +
//            "SPEED 4.0\n" +
//            "CLS\n" +
//            "DIRECTION 1\n" +
//            "REFERENCE 0,0\n" +
//            "SHIFT 0\n" +
//            "TEXT 0,15,\"TSS24.BF2\",0,1,1,\"     万宝路(硬冰爵)     \"\n" +
//            "TEXT 0,39,\"TSS24.BF2\",0,1,1,\"-------------------------\"\n" +
//            "TEXT 0,63,\"TSS24.BF2\",0,1,1,\"规格:20                  \"\n" +
//            "TEXT 0,87,\"TSS24.BF2\",0,1,1,\"生产日期:2019-08-08\"\n" +
//            "BARCODE 0,111,\"128\",30,1,0,2,2,\"6901028196468\"\n" +
//            "TEXT 0,170,\"TSS24.BF2\",0,1,1,\"零售价: ￥25              \"\n" +
//            "PRINT 1,1\n" +
//            "EOP\n";

    public static void main(String[] args) {

        System.out.println("StringToHex16:main:");
        String hexStr_1 = "1130300A91EE602";
        String decStr_1 = "77408919967688194";


        byte[] recvbuffer = new byte[2048];
        recvbuffer[0] = 123;
        recvbuffer[2] = 124;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < recvbuffer.length; i++) {
            stringBuffer.append(recvbuffer[i]);
            stringBuffer.append(",");
        }
        String str2 = new String(recvbuffer);
        System.out.println("str2:"+str2);
        System.out.println("str2:"+stringBuffer.toString());

        System.out.println(strTo16(printStr));
        System.out.println("----------->>>>>>");
//        PrintUtil.print(strTobytes(printStr));
        String s = strTobytes(printStr);
        System.out.println(s);
        String substring = s.substring(0, s.length() / 2);
        String substring1 = s.substring(s.length() / 2, s.length() - 1);
        System.out.println(substring);
        System.out.println(substring1);
        System.out.println("----------->>>>>>");
        String s2 = null;
        try {
            s2 = str2HexStr(printStr);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(s2);
        System.out.println("----------->>>>>>结果");
//        String s1 = strTo16(string2Unicode(printStr));
        System.out.println(hexToDec(s2));


    }

    /**
     * 16进制直接转换成为字符串(无需Unicode解码)
     * @param hexStr
     * @return
     */
    public static String hexToDec(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        try {
            return new String(bytes,"gbk");
//            System.err.println( new String(bytes,"gbk"));
//            return new String(bytes,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转换unicode
     */
    public static String string2Unicode(String string) {
    StringBuffer unicode = new StringBuffer();
    for (int i = 0; i < string.length(); i++) {
    // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
    }
    return unicode.toString();
    }

    /**
     * 字符串转化成为16进制字符串
     *
     * @param s
     * @return
     */
    public static String strTo16(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }

    public static String str2HexStr(String str) throws UnsupportedEncodingException {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes("GBK");
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        // sb.append(' ');
        }
        return sb.toString().trim();
    }


    private static String charsetName = "gbk";

//    private static byte[] strTobytes(String str) {
    private static String strTobytes(String str) {
        byte[] b = null;
        byte[] data = null;
        StringBuilder sb = new StringBuilder("");
        try {
            b = str.getBytes("utf-8");
            if (charsetName == null | charsetName == "") {
                charsetName = "gbk";
            }

            data = (new String(b, "utf-8")).getBytes(charsetName);
            for (byte bit:b) {
                sb.append(bit);
            }
        } catch (UnsupportedEncodingException var4) {
            var4.printStackTrace();
        }


        return sb.toString().trim();
    }


    /**
     * 十六进制String字符串转byte数组
     * @param hex
     * @return
     */
//    public static byte[] hexTobytes(String hex){
//        if (hex==null||hex.length()==0){//非空判断
//            return null;
//        }
//        char[] chars = hex.toCharArray();//String转换为char数组
//        byte[] bytes=new byte[chars.length/2];//如果chars中字符不是偶数个则忽略最后一个数字
//
//        for (int i = 0; i < bytes.length; i++) {
//            bytes[i] = (byte) Integer.parseInt("" + chars[i * 2] + chars[i * 2 + 1], 16);//十六进制转位十进制
//        }
//        return bytes;
//    }

    /**
     * Hex转byte,hex只能含两个字符，如果是一个字符byte高位会是0
     */
//    public static byte hexTobyte(String hex) {
//        return (byte)Integer.parseInt(hex, 16);
//    }
    /**
     * Hex转byte[]，两种情况，Hex长度为奇数最后一个字符会被舍去
     */
    public static byte[] hexTobytes(String hex) {
        if (hex.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hex.length() / 2];
            int j = 0;
            for(int i = 0; i < hex.length(); i+=2) {
                result[j++] = (byte)Integer.parseInt(hex.substring(i,i+2), 16);
            }
            return result;
        }
    }







    //java 10进制转16进制字符串

    // 使用1个字节表示 ----待验证
    public static String numToHex8(int b) {
        return String.format("%02x", b);// 2表示需要两个16进制数
    }

    // 使用2个字节表示
    public static String numToHex16(int b) {
        return String.format("%04x", b);
    }

    /**
     * ----已验证。。。
     * 将10进制整型转为16进制字符串 （使用4个字节表示）
     * @param b 10进制整型
     * @return 16进制字符串
     */
    public static String numToHex32(long b) {
        return String.format("%08x", b);
    }

    /**
     * ----待验证
     * 使用8个字节表示
     * @param b 10进制整型
     * @return 16进制字符串
     */
    public static String numToHex64(long b) {
        return String.format("%016x", b);
    }

    /**
     * 获取16进制的底图标签值
     * @param b 10进制底图标签值
     * @return 16进制底图标签值
     */
    public static String hexString(long b) {
        return "0x".concat(numToHex32(b));
    }


}
